//
//  CustomerInformationViewController.m
//  Edumap
//
//  Created by 辰领科技 on 16/7/25.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "CustomerInformationViewController.h"
#import "CustomerInformationTableViewCell.h"
#import "certificationViewController.h"

static NSString *CellIdentifier = @"CustomerInformationTableViewCell";


@interface CustomerInformationViewController ()
<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroview;
@property (strong, nonatomic) IBOutlet UILabel *label;
@property (strong, nonatomic) IBOutlet UITextField *textField;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSArray *array;
@end

@implementation CustomerInformationViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self customerInformation];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewLayout];
}


- (void)initViewLayout {
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroview];
    _scroview.contentSize = flexibleSize(CGSizeMake(320, 505), NO);
    
    self.label.layer.cornerRadius = 5*VerticalRatio();
    self.label.layer.borderColor = [UIColor colorWithWhite:0.902 alpha:1.000].CGColor;
    self.label.layer.borderWidth = 1;
    [self.textField setValue:[UIColor colorWithWhite:0.627 alpha:1.000]
                  forKeyPath:@"_placeholderLabel.textColor"];
    
    [self.tableView registerNib:[UINib nibWithNibName:CellIdentifier bundle:nil] forCellReuseIdentifier:CellIdentifier];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    _textField.delegate =self;
}

- (IBAction)back:(id)sender {
    [self backBtn];
}

#pragma mark - 推广人员客户信息
- (void)customerInformation {
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"memberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    if (_searchStr) {
        [dic setObject:_searchStr forKey:@"msceName"];
    }
    if (_textField.text.length>0) {
        [dic setObject:_textField.text forKey:@"msceName"];
    }
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:JY_mallStoreCertificationList withHttpType:@"POST" withTag:JY_JY_mallStoreCertificationList];
}



#pragma mark - <UITableViewDelegate&&UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//定义 UITableView cell 的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}

//定义 UITableView cell 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90*VerticalRatio();
}

//定义 UITableView cell 展示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomerInformationTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell= [[CustomerInformationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        
    }
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    
    cell.titleLabel.text = [NSString stringWithFormat:@"%@",_array[indexPath.row][@"msceName"]];
    [cell.phoneNumberLabel setTitle:[NSString stringWithFormat:@" %@",_array[indexPath.row][@"mscePhone"]] forState:UIControlStateNormal];
    [cell.addressLabel setTitle:[NSString stringWithFormat:@" %@",_array[indexPath.row][@"msceAddr"]] forState:UIControlStateNormal];
    
    return cell;
}

//定义 UITableView cell 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    certificationViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"certification"];
    page.msceId = [NSString stringWithFormat:@"%@",_array[indexPath.row][@"msceId"]];
    page.certificationIdentifier = 0;
    
    [[Common getNavigation]pushViewController:page];
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self customerInformation];
    [self hideKeyBoard];
    return YES;
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    
    if (tag==JY_JY_mallStoreCertificationList) {
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _array = [NSArray array];
            _array = database[@"result"][@"rows"];
            [self.tableView reloadData];
        }
    }
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}

@end
